package com.service.sale;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.dao.sale.ProductDao;
import com.enums.BtnDbPermissionEnum;
import com.enums.SysModuleEnum;
import com.model.sale.ProductModel;
import com.model.sys.SysUser;

@Service
public class ProductService {
	static final Logger logger = LoggerFactory.getLogger(ProductService.class);

	@Autowired
	ProductDao productDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {// 非管理员
			if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.sale.getValue())) {
				// 销售管理员
			} else if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.BatteryChemical.getValue())) {
				// 锂电池管理员
				queryMap.put("draOrgName", BtnDbPermissionEnum.BatteryChemical.getName());
				queryMap.put("dracreateId", sysUser.getUserid());
			} else if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.FineChemical.getValue())) {
				// 精细管理员
				queryMap.put("draOrgName", BtnDbPermissionEnum.FineChemical.getName());
				queryMap.put("dracreateId", sysUser.getUserid());
			} else if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				// 销售部门权限
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<ProductModel> list = productDao.selectList(queryMap);
		int count = productDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String proId) {
		HttpResult httpResult = new HttpResult();
		ProductModel productModel = productDao.load(proId);
		httpResult.setData(productModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(ProductModel productModel) {
		if (productModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(productModel.getProId())) {
			productModel.setProId(UUID.randomUUID().toString());
			result = productDao.insert(productModel);
		} else {
			result = productDao.update(productModel);
		}
		if (result > 0) {
			httpResult.setMsg("保存成功！");
			httpResult.setData(productModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("保存失败！");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String proId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(proId)) {
			return HttpResult.error("参数不能为空");
		}
		int result = productDao.delete(proId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {// 非管理员
			if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.sale.getValue())) {
				// 销售管理员
			} else if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.BatteryChemical.getValue())) {
				// 锂电池管理员
				queryMap.put("draOrgName", BtnDbPermissionEnum.BatteryChemical.getName());
				queryMap.put("dracreateId", sysUser.getUserid());
			} else if (sysUser.getBtnPermission() != null
					&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.FineChemical.getValue())) {
				// 精细管理员
				queryMap.put("draOrgName", BtnDbPermissionEnum.FineChemical.getName());
				queryMap.put("dracreateId", sysUser.getUserid());
			} else if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				// 销售部门权限
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<ProductModel> list = productDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), ProductModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
